#include "fdiskio.h"
#include <stdint.h>
#include "dev_flash.h"
/*-----------------------------------------------------------------------*/
/* Correspondence between physical drive number and physical drive.      */

#define FLASH		     0
#define SECTOR_SIZE     4096
//static uint32_t try_times;



/*!
    \brief Inicializes a Drive 
    \param[in]  none
    \param[out] none
    \retval
*/
DSTATUS disk_initialize (BYTE drv)    /* Physical drive nmuber (0..) */
{
    DSTATUS stat = STA_NOINIT;
    stat &= ~STA_NOINIT;
    return stat;
}



/*!
    \brief Return Disk Status    
    \param[in]  none
    \param[out] none
    \retval
*/
DSTATUS disk_status (BYTE drv)  /* Physical drive nmuber (0..) */
{
	DSTATUS stat = STA_NOINIT;
	// int result;

	switch (drv) {
	    case FLASH :
            stat &= ~STA_NOINIT;
        break;

	    default:
        break;
	}
	return stat;
}



/*!
    \brief Read Sector   
    \param[in]  none
    \param[out] none
    \retval
*/
DRESULT disk_read (BYTE drv, BYTE *buff, DWORD sector, BYTE count)
{
	DRESULT res = RES_PARERR;

	switch (drv) {
	    case FLASH :
            spi_flash_buffer_read_block(buff, sector*SECTOR_SIZE, SECTOR_SIZE);
            res = RES_OK;
            break;
        default:
        break;
	}
	return res;
}



/*!
    \brief Write Sector(s)   
    \param[in]  none
    \param[out] none
    \retval
*/
DRESULT disk_write(BYTE drv, BYTE *buff, DWORD sector, BYTE count)
{
	DRESULT res = RES_PARERR;

	switch (drv) {
	    case FLASH :
            spi_flash_sector_erase(sector*SECTOR_SIZE);
            spi_flash_buffer_write(buff, sector*SECTOR_SIZE, SECTOR_SIZE);
            res = RES_OK;
		break;

		default:
        break;
	}
	return res;
}



/*!
    \brief Eraser Sector   
    \param[in]  none
    \param[out] none
    \retval
*/
DRESULT disk_eraser (BYTE drv,  DWORD sector)
{
	DRESULT res = RES_PARERR;

	switch (drv) {
	    case FLASH :
            spi_flash_sector_erase(sector*SECTOR_SIZE);
            res = RES_OK;
		break;

		default:
        break;
	}
	return res;
}




/*!
    \brief Read buffer   
    \param[in]  none
    \param[out] none
    \retval
*/
DRESULT disk_bread (BYTE drv, BYTE *buff, DWORD addr, DWORD count)
{
	DRESULT res = RES_PARERR;

	switch (drv) {
	    case FLASH :
            spi_flash_buffer_read_block(buff, addr, count);
            res = RES_OK;
            break;
        default:
        break;
	}
	return res;
}



/*!
    \brief Write Sector(s)   
    \param[in]  none
    \param[out] none
    \retval
*/
DRESULT disk_bwrite(BYTE drv, BYTE *buff, DWORD addr, DWORD count)
{
	DRESULT res = RES_PARERR;

	switch (drv) {
	    case FLASH :
            spi_flash_buffer_write(buff, addr, count);
            res = RES_OK;
		break;

		default:
        break;
	}
	return res;
}




/*!
    \brief Miscellaneous Functions   
    \param[in]  none
    \param[out] none
    \retval
*/
DRESULT disk_ioctl (
	BYTE drv,		/* Physical drive nmuber (0..) */
	BYTE ctrl,		/* Control code */
	void *buff		/* Buffer to send/receive control data */
)
{
	DRESULT res = RES_OK;
	// int result;

	switch (drv) {
	    case FLASH :
            switch (ctrl)
            {
            case GET_SECTOR_COUNT:/* constant-expression */
                *(uint32_t *)buff = 4096;
                break;
            
            default:
                break;
            }

		return res;

        default:
        break;
	}
	return RES_PARERR;
}

